<?php

namespace app\facade;

use think\facade\Env;
use app\facade\Redis;

class Mns
{


    private $time = 300;

    public function send($mobile = null, $data = [])
    {
        $SendRes = [
            'status' => false,
            'msg' => '发送失败'
        ];
        $MnsRds = 'MnsRds' . $mobile;
        $MnsTime = Redis::get($MnsRds);
        if ($MnsTime) {
            $SendRes['msg'] = '短信发送频繁请' . ($MnsTime - time()) . 's后发送';
            return $SendRes;
        }
        $code = rand(100000, 999999);
        $content = '【嘀嗒回收】您的验证码是' . $code . ',该验证码' . ($this->time / 60) . '分钟内有效，请勿泄露给他人。';
        // $content = urlencode($content);
        $url = "http://m.5c.com.cn/api/send/index.php?";
        $data = array(
            'username' => '13125136474',
            'password_md5' => md5('dida888'),
            'apikey' => '3e381b74344486f2fa264adc32a492c4',
            'mobile' => '+86' . $mobile,
            'content' => $content,
            'encode' => 'ute-8',
        );
        $result = $this->curlSMS($url, $data);
        if (strpos($result, 'success') !== false) {
            //查询是否
            $res = Redis::setex($mobile, $this->time, $code);
            if ($res) {
                Redis::setex($MnsRds, 60, time() + 60);
                $SendRes = [
                    'status' => true,
                    'msg' => '发送成功'
                ];
                return $SendRes;
            }
        }
        return $SendRes;
    }

    public function sendmessage($getmobile = '', $ordersn = '', $name = '', $mobile = '', $address = '', $appointment = '')
    {
        $content = '【嘀嗒回收】您有新的订单，请尽快接单;订单编号:' . $ordersn . ',用户名:' . $name . '，联系电话:' . $mobile . '，联系地址:' . $address . '，预约时间:' . $appointment;
        $url = "http://m.5c.com.cn/api/send/index.php?";
        $data = array(
            'username' => '13125136474',
            'password_md5' => md5('dida888'),
            'apikey' => '3e381b74344486f2fa264adc32a492c4',
            'mobile' => '+86' . $getmobile,
            'content' => $content,
            'encode' => 'ute-8',
        );
        $result = $this->curlSMS($url, $data);
        if (strpos($result, 'success') !== false) {
            return true;
        }
        return false;
    }

    function curlSMS($url, $post_fields = array())
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
        $data = curl_exec($ch);
        curl_close($ch);
        $res = explode("\r\n\r\n", $data);
        return $res[1];
    }

    public function checkCode($code = null, $mobile = null)
    {
        if (!Env::get('sms.open')) {
            return true;
        }
        $check = Redis::get($mobile);
        if ($check && ($mobile !== null)) {
            return $check == $code ? true : false;
        }
        return false;
    }
}
